package com.faf.tools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DbUtils
{
	private static String url = null;
	private static String user = null;
	private static String pwd = null;
	
	static 
	{
		try
		{
			Properties p = new Properties();
			p.load(DbUtils.class.getResourceAsStream("configs.properties"));


			url = p.getProperty("url");
			user = p.getProperty("user");
			pwd = p.getProperty("pwd");
			

		    Class.forName(p.getProperty("driver"));
		    
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection()
	{
		Connection con = null;
		
		try
		{
			con = DriverManager.getConnection(url,user,pwd);
		
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		return con;
	}
	
	public static void close(Connection con, PreparedStatement ps, ResultSet rs)
	{
		if(rs != null)
		{
			try
			{
				rs.close();
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		if(ps != null)
		{
			try
			{
				ps.close();
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		if(con != null)
		{
			try
			{
				con.close();
				
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
}
